import _isEmpty from 'lodash/isEmpty'
import { isArray } from 'xe-utils'
export default {
  props: {
    model: {
      type: Object,
      default: () => { }
    },
    params: {
      default: 'form'
    },
  },
  data () {
    return {}
  },

  watch: {
    model: {
      handler (newValue, oldValue) {
        let params = this.$route.query
        let querys = {}
        Object.keys(newValue).map((key) => {
          let item = newValue[key]
          if (item != 0 && !item) return
          if (isArray(item) && item.length == 0) return // 兼容select多选，初始值为空数组情况
          querys[key] = item
        })

        if (!_isEmpty(querys)) {
          this.$router.replace({
            query: {
              ...params,
              queryParams: JSON.stringify(querys),
            },
          })
        } else {
          this.$router.replace({
            query: {},
          })
          if (params.queryParams) {
            delete params.queryParams
          }
          if (_isEmpty(params)) {
            this.$router.replace()
          } else {
            this.$router.replace({
              query: {
                ...params,
              },
            })
          }
        }
      },
      deep: true,
    }
  },

  created () {
    /**
     * 路由链接带查询参数
     */
    let { queryParams } = this.$route.query
    if (queryParams) {
      let filter = JSON.parse(queryParams)
      this.$parent[this.params] = filter
    }
  },

}
